package com.javiercp.ayudaapp.location;

import com.javiercp.ayudaapp.MyConstants;

import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.util.Log;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

public class LocationHelper {

	public static String locationToString(Location location) {
		String retValue;
		
		retValue = "Location info\n" +
					"\tTime: " + new Date(location.getTime()).toLocaleString() + "\n" +
					"\tLongitude: " + location.getLongitude() + "\n" +
					"\tLatitude: " + location.getLatitude() + "\n";
		
		if (location.hasAltitude()) {
			retValue = retValue + "\tAltitude: " + location.getAltitude() + "\n";
		}
		if (location.hasBearing()) {
			retValue = retValue + "\tBearing: "+ location.getBearing() + " degrees East of true North.\n";
		}
		if (location.hasSpeed()) {
			retValue = retValue + "\tSpeed: "+ location.getSpeed() + " meters/second.\n";
		}
			
		retValue = retValue + "\tAccuracy: " + (location.hasAccuracy()? location.getAccuracy() + " meters." : "No accuracy data.") + "\n";
		
		return retValue;
	}
	
	public static String reverseGeolocate(Context context, Location location) {
		String retValue = "";
		
		Geocoder geocoder = new Geocoder(context);
		
		try {
			List<Address> foundAddresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
			
			retValue = "Geolocation info\n";
			
			Address foo = foundAddresses.get(0);
			int var = foundAddresses.get(0).getMaxAddressLineIndex();
			for (int i=0 ; i<var; i++) {
				retValue = retValue + "\t" + foo.getAddressLine(i) + "\n";
			}
			
			//retValue = foundAddresses.get(0).toString();
		}
		catch (Exception ex) {
		}
		
		return retValue;
	}
	
	
	public static String serialize(Location location) {
		LocationData locData = new LocationData(location.getAltitude(), location.getLatitude(), location.getLongitude(),
				location.getSpeed(), location.getBearing(), location.getAccuracy(), location.getTime(), location.getProvider(),
				location.hasAltitude(), location.hasSpeed(), location.hasBearing(), location.hasAccuracy());
		
		Serializer serializer = new Persister();	
		Writer writer = new StringWriter();
		
		try
		{
			serializer.write(locData, writer);
		}
		catch (Exception ex)
		{
			Log.e(MyConstants.LOGTAG, "Error serializing object.", ex);
		}
		
		return writer.toString();
	}
	
	public static Location deserialize(String serializedLocation) {	
		if (serializedLocation == "") return null;
		
		Reader reader = new StringReader(serializedLocation);
		Serializer serializer = new Persister();
		
		LocationData locData;	
		try
		{
			locData = serializer.read(LocationData.class, reader, false);	
		}
		catch (Exception ex)
		{
			Log.e(MyConstants.LOGTAG, "Error deserializing object.", ex);
			locData = null;
		}
		
		if (locData != null) {
			Location retValue = new Location(locData.getProvider());
			if (locData.getHasAltitude()) retValue.setAltitude(locData.getAltitude());
			retValue.setLatitude(locData.getLatitude());
			retValue.setLongitude(locData.getLongitude());
			if (locData.getHasSpeed()) retValue.setSpeed(locData.getSpeed());
			if (locData.getHasBearing()) retValue.setBearing(locData.getBearing());
			if (locData.getHasAccuracy()) retValue.setAccuracy(locData.getAccuracy());
			retValue.setTime(locData.getTime());
			
			return retValue;
		}
		else {
			return null;
		}
	}
	
	
}
